package com.moqianfan.mcuassistant.fragment.play;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.BaseFragmentAdapter;
import com.moqianfan.mcuassistant.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/5.
 * 娱乐
 */

public class PlaysFragment extends BaseFragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    private ViewPager mPagerPlays;
    private RadioGroup mRadioGroup;
    private RadioButton radioBtnTxt;
    private RadioButton radioBtnPic;
    private RadioButton radioBtnNew;

    @Override
    public View findViews(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_play, container, false);
        mPagerPlays = (ViewPager) view.findViewById(R.id.vp_play);
        mRadioGroup = (RadioGroup) view.findViewById(R.id.vp_play_top);

        radioBtnTxt = (RadioButton) view.findViewById(R.id.radio_play_top_txt);
        radioBtnPic = (RadioButton) view.findViewById(R.id.radio_play_top_pic);
        radioBtnNew = (RadioButton) view.findViewById(R.id.radio_play_top_news);
        return view;
    }

    @Override
    public void initViews() {
        List<BaseFragment> mFragments = new ArrayList<>();
        mFragments.add(new ReadFragment());
        mFragments.add(new JokeFragment());
        mFragments.add(new ITFragment());
        mPagerPlays.setAdapter(new BaseFragmentAdapter(getChildFragmentManager(), mFragments));
        mPagerPlays.setCurrentItem(0);
        mPagerPlays.setOffscreenPageLimit(3);
        mPagerPlays.addOnPageChangeListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (mPagerPlays.getCurrentItem()) {
                case 0:
                    radioBtnPic.setChecked(true);
                    break;
                case 1:
                    radioBtnTxt.setChecked(true);
                    break;
                case 2:
                    radioBtnNew.setChecked(true);
                    break;
//                case 2:
//                    radioBtnGil.setChecked(true);
//                    break;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.radio_play_top_pic:
                mPagerPlays.setCurrentItem(0);
                break;
            case R.id.radio_play_top_txt:
                mPagerPlays.setCurrentItem(1);
                break;
            case R.id.radio_play_top_news:
                mPagerPlays.setCurrentItem(2);
                break;
//            case R.id.radio_play_top_girl:
//                mPagerPlays.setCurrentItem(2);
//                break;

        }
    }
}
